package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.system.domain.Receivable;
import com.ruoyi.system.mapper.ReceivableMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.FinanceAccountsMapper;
import com.ruoyi.system.domain.FinanceAccounts;
import com.ruoyi.system.service.IFinanceAccountsService;

/**
 * 财务记账Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-19
 */
@Service
public class FinanceAccountsServiceImpl implements IFinanceAccountsService 
{
    @Autowired
    private FinanceAccountsMapper financeAccountsMapper;

    /**
     * 查询财务记账
     * 
     * @param id 财务记账主键
     * @return 财务记账
     */
    @Override
    public FinanceAccounts selectFinanceAccountsById(Long id)
    {
        return financeAccountsMapper.selectFinanceAccountsById(id);
    }

    /**
     * 查询财务记账列表
     * 
     * @param financeAccounts 财务记账
     * @return 财务记账
     */
    @Override
    public List<FinanceAccounts> selectFinanceAccountsList(FinanceAccounts financeAccounts)
    {
        return financeAccountsMapper.selectFinanceAccountsList(financeAccounts);
    }

    @Autowired
    private ReceivableMapper receivableMapper;
    /**
     * 新增财务记账
     * 
     * @param financeAccounts 财务记账
     * @return 结果
     */
    @Override
    public int insertFinanceAccounts(FinanceAccounts financeAccounts)
    {
        //更新应收款记录的收款金额
        Receivable receivable = receivableMapper.selectReceivableById(financeAccounts.getReceivableId());
        BigDecimal actualReceivedAmount = financeAccounts.getPaymentAmount().add(receivable.getActualReceivedAmount());
        receivable.setActualReceivedAmount(actualReceivedAmount);
        receivableMapper.updateReceivable(receivable);

        //保存收款记账信息
        financeAccounts.setPaymentTime(new Date());
        return financeAccountsMapper.insertFinanceAccounts(financeAccounts);
    }

    /**
     * 修改财务记账
     * 
     * @param financeAccounts 财务记账
     * @return 结果
     */
    @Override
    public int updateFinanceAccounts(FinanceAccounts financeAccounts)
    {
        return financeAccountsMapper.updateFinanceAccounts(financeAccounts);
    }

    /**
     * 批量删除财务记账
     * 
     * @param ids 需要删除的财务记账主键
     * @return 结果
     */
    @Override
    public int deleteFinanceAccountsByIds(Long[] ids)
    {
        return financeAccountsMapper.deleteFinanceAccountsByIds(ids);
    }

    /**
     * 删除财务记账信息
     * 
     * @param id 财务记账主键
     * @return 结果
     */
    @Override
    public int deleteFinanceAccountsById(Long id)
    {
        return financeAccountsMapper.deleteFinanceAccountsById(id);
    }
}
